using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Testudo.Mongo;
using Testudo.Services;
using Testudo.WebApp.Hubs;
using Testudo.WebApp.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

//builder.Services.AddSignalR().AddJsonProtocol(options =>
//{
//    options.PayloadSerializerOptions.PropertyNamingPolicy = null;
//});

builder.Services.AddSignalR().AddNewtonsoftJsonProtocol(options =>
{
    options.PayloadSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
    options.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    options.PayloadSerializerSettings.ContractResolver = new DefaultContractResolver();
    options.PayloadSerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
    options.PayloadSerializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
    options.PayloadSerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Unspecified;
    options.PayloadSerializerSettings.DateFormatString = "yyyy/MM/dd HH:mm:ss";
});

builder.Services.RegisterServices();
builder.Services.AddSingleton<IWcsProxyService, WcsProxyService>();

builder.Services.RegisterMongo();
builder.Services.AddSingleton(builder.Configuration.GetSection("MongoConfig").Get<MongoConfig>());

builder.Services.AddAutoMapper(typeof(Program).Assembly);

//builder.Services.AddScoped<IRepository<ScenarioConfig, ScenarioConfigVM, CreateScenarioConfigVM, UpdateScenarioConfigVM>, MongoRepository<ScenarioConfig, ScenarioConfigVM, CreateScenarioConfigVM, UpdateScenarioConfigVM>>();

builder.Services.AddCors(
    options => options.AddDefaultPolicy(
        policy =>
            policy
                .SetIsOriginAllowed(_ => true)
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials()
    ));

builder.Services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
        options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
        options.SerializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
        options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Unspecified;
        options.SerializerSettings.DateFormatString = "yyyy/MM/dd HH:mm:ss";
    });
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGenNewtonsoftSupport();

builder.Services.AddHostedService<WcsServiceWorker>();
builder.Services.AddSingleton<IWcsProxyService, WcsProxyService>();
builder.Services.AddSingleton<IScenarioProxy, ScenarioProxy>();

var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.UseCors();

app.MapHub<WcsProxyHub>("/hubs/wcs-proxy");

app.Run();